layui.config({
    base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        $ = layui.jquery;

    //http://localhost/ci/public/static/json/usersList.json
    //http://localhost/ci/admin/user/imp_index
    //加载页面数据
    var usersData = '';
    $.get("http://localhost/ci/admin/role/imp_index", {'token':localStorage.token,'user_token':localStorage.user}, function(data){
        //console.log(JSON.parse(data));
        usersData = JSON.parse(data);
        if(data == '0'){
            top.location = 'http://localhost/ci/login/login/login';//整个页面进行跳转
        }
    })

    //alert(parent.$('.ci_data').val());
    var r_data ='';
    $.get("http://localhost/ci/admin/userRole/test", {'user_id':parent.$('.ci_data').val(),'token':localStorage.token,'user_token':localStorage.user}, function(data){
        //console.log(JSON.parse(data));
        r_data = JSON.parse(data);
        console.log(data)
        roleschanges(r_data);
    })

    function roleschanges(r_data){
        //console.log(r_data);
        //渲染数据
        function renderDate(userRoles,roles){
            var dataHtml = '';
            console.log(roles.length);
            //console.log(userRoles[0]['user_id'])
            if(roles.length != 0){
                var dataHtml2 = '';
                for(var i=0;i<roles.length;i++){
                    var flag = 0;
                    if(userRoles.length!=0){
                        for(var j = 0; j < userRoles.length; j++){
                            if(userRoles[j].role_id == roles[i].role_id){
                                flag = 1;
                            }
                        }
                    }
                    if(flag){
                        dataHtml2+=
                            '<input class='+i+' type="checkbox"  name="role_id" value='+roles[i].role_id+' title='+roles[i].role_name+' checked>'
                    } else {
                        dataHtml2+=
                            '<input class='+i+' type="checkbox"  name="role_id" value='+roles[i].role_id+' title='+roles[i].role_name+'>'
                    }
                }
                dataHtml += '<tr>'
                    +'<td>'
                    +   dataHtml2
                    +'</td>'
                    +'</tr>';
            }else{
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            return dataHtml;
        }

        //分页
        var nums = 10; //每页出现的数据量
        laypage({
            cont : "page",
            pages : Math.ceil(usersData.length/nums),
            jump : function(obj){
                $(".roles_content").html(renderDate(r_data['userRoles'],r_data['roles']));
                $('.roles_list thead input[type="checkbox"]').prop("checked",false);
                form.render();
            }
        })
    }

    //判断是否改变了被选属性
    $("body").on("click",".layui-form-checkbox",function(){
        if($(this).prev().attr('checked')){
            $(this).prev().removeAttr("checked");
        }else{
            $(this).prev().attr('checked','checked');
        }

    })


    $(".roleEdit").click(function(){  //更改角色
        console.log(usersData.length);
        console.log(parent.$('.ci_data').val())
        layer.confirm('确定更改此用户吗？',{icon:3, title:'提示信息'},function(index){
                var addUserRole = '{';
                var count = 0;
                var flag = 0;
                for(var i=0;i<usersData.length;i++){
                    if($('.'+i).attr('checked')){
                        if (flag == 0) {
                            addUserRole += '"' + count + '":"' + $('.'+i).val() + '"';
                            flag++;
                            count++;
                        } else {
                            addUserRole += ',"' + count + '":"' + $('.'+i).val() + '"';
                            count++;
                        }
                    }
                }
                addUserRole+='}';
                $.ajax({
                    url : "http://localhost/ci/admin/userRole/add_user_role",
                    type : "post",
                    data : {'user_id':parent.$('.ci_data').val(),'data':addUserRole, 'token':localStorage.token,'user_token':localStorage.user},
                    dataType : "json"
                })
            //alert(addUserRole)
            top.layer.msg("用户更改成功！");
            //重新加载页面
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();

        });

    })

})